package com.qf.mybatis;

import com.qf.bean.Menu;
import com.qf.mapper.MenuMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class UseMybatisSelect {
    public static void main(String[] args) throws IOException {
        //1·读取配置
        InputStream stream = Resources.getResourceAsStream("mybatis_config.xml");
        //2·创建sqlSessionFactoryBuilder
        SqlSessionFactoryBuilder factoryBuilder = new SqlSessionFactoryBuilder();
        //3·创建sqlSessionFactory
        SqlSessionFactory sqlSessionFactory = factoryBuilder.build(stream);
        //4·创建sqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //5·获取mapper
        MenuMapper mapper = sqlSession.getMapper(MenuMapper.class);
        //6·调用mapper的方法
        List<Menu> menus = mapper.queryMenu(3);
        System.out.println("menus"+menus);
        //6·关闭资源
        sqlSession.close();
    }
}

